package com.blockmeta.bbs.businesslibrary.websocket;


import com.blankj.utilcode.util.LogUtils;
import com.blockmeta.bbs.businesslibrary.base.eventbus.EventBusBase;
import com.blockmeta.bbs.businesslibrary.business.DataManager;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantReadWriteLock;


public class ConnectCenter extends EventBusBase implements IFlowManager {


    protected Map<Market, ConnectManager> m_mapAppServer;     // AppServer List
    protected ConnectManager m_loginServer;
    protected Market m_activeMarket;
    private ReentrantReadWriteLock m_lock;
    private Map<String, ConnectInfo> m_mapLockAppIPs;
    private static ConnectCenter m_ConnectCenter;

    ConnectCenter() {
        m_mapAppServer = new ConcurrentHashMap<Market, ConnectManager>();

        m_lock = new ReentrantReadWriteLock();

        m_activeMarket = DataManager.getDataManager().getQuoteCenter().getMarket("coin");

        m_mapLockAppIPs = new ConcurrentHashMap<String, ConnectInfo>();

    }

    public static ConnectCenter getConnectCenter() {
        if (m_ConnectCenter == null) {
            synchronized (DataManager.class) {
                if (m_ConnectCenter == null) {
                    m_ConnectCenter = new ConnectCenter();
                }
            }
        }
        return m_ConnectCenter;
    }


    public boolean isConnectOK(Market market) {
        ConnectManager mgr = getConnectManager(market);
        if (mgr != null) {
            return mgr.isConnectOK();
        }
        return false;
    }

    public boolean isActiveConnectOK() {
        ConnectManager mgr = getConnectManager(m_activeMarket);
        if (mgr != null) {
            return mgr.isConnectOK();
        }
        return false;
    }


    public ConnectManager getConnectManager(Market market) {
        ConnectManager mgr = null;
        try {
            m_lock.readLock().lock();
            mgr = m_mapAppServer.get(market);
        } finally {
            m_lock.readLock().unlock();
        }
        return mgr;
    }


    /**
     * @param marketCode
     * @param info
     */
    public void initAppServer(final String marketCode, ConnectInfo info
    ) {
        try {
            m_lock.writeLock().lock();
            Market market = DataManager.getDataManager().getQuoteCenter().getMarket(marketCode);
            ConnectManager mgr = m_mapAppServer.get(market);
            ConnectManager.ConnType type = ConnectManager.ConnType.Active;
            if (mgr == null) {
                mgr = new ConnectManager(ConnectManager.ServerType.Quote, type, market);
                m_mapAppServer.put(market, mgr);
            }

            mgr.loadServer(info);

        } catch (Exception e) {
            LogUtils.d("author"+ e.getMessage());
        } finally {
            m_lock.writeLock().unlock();
        }
    }

    public ConnectManager getActiveConnectManager() {
        if (m_activeMarket != null) {
            return getConnectManager(m_activeMarket);
        }
        return null;
    }

    public void onResume() {
        // 1. 如果連線中 且已經suspend 則resume
        // 2. 如果已斷線 則重新連線
        ConnectManager activeConnectManager = getActiveConnectManager();
        if (activeConnectManager != null) {
            if (activeConnectManager.isConnectOK()) {
                activeConnectManager.sendResume();
            } else {
                activeConnectManager.startConnect();
            }
        }

    }

    public void onPause() {
        try {
            m_lock.readLock().lock();
            for (Map.Entry<Market, ConnectManager> entry : m_mapAppServer.entrySet()) {
                ConnectManager mgr = entry.getValue();
                mgr.closeConnect(false);
            }
        } finally {
            m_lock.readLock().unlock();
        }
    }


    @Override
    public void onCreate() {

    }

    @Override
    public void onConnectCreate() {
        initConnect();

        DataManager.getDataManager().onConnectCreate();
    }

    protected void initConnect() {
        ConnectManager activeConnectManager = getActiveConnectManager();
        if (activeConnectManager != null) {
            activeConnectManager.initConnect();
        }
    }

    public void closeAllConnect()
    {
        try
        {
            m_lock.readLock().lock();
            for (Map.Entry<Market, ConnectManager> entry : m_mapAppServer.entrySet())
            {
                ConnectManager mgr = entry.getValue();
                mgr.closeConnect(false);
            }
        }
        finally
        {
            m_lock.readLock().unlock();
        }
    }
}